﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SearchTextFiles
{
    class Searchtxt
    {
        static void SearchInTextFiles(string word, string dir, StreamWriter output)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            foreach (FileInfo fInfo in dirInfo.GetFiles())
            {
                if (fInfo.Extension == ".txt")
                {
                    int line = 1;
                    using (StreamReader input = new StreamReader(fInfo.FullName))
                    {
                        string fileLine = input.ReadLine();
                        while (fileLine != null)
                        {
                            if (fileLine.IndexOf(word) > -1)
                            {
                                output.WriteLine(fInfo.FullName + " " + line);
                                break; ;
                            }
                            fileLine = input.ReadLine();
                            line++;
                        }
                    }
                }
            }
            foreach (DirectoryInfo d in dirInfo.GetDirectories())
            {
                SearchInTextFiles(word, d.FullName, output);
            }
        }

        static void Main()
        {
            using (StreamWriter output = new StreamWriter("result.txt"))
            {
                SearchInTextFiles("Ivan", "dir", output);
            }
        }
    }
}
